package com.sunfield.windpower.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunfield.baseframe.base.MyRecyclerAdapter;
import com.sunfield.windpower.R;
import com.sunfield.windpower.activity.WebViewActivity_;
import com.sunfield.windpower.bean.GuideInfo;
import com.sunfield.windpower.http.HttpClient;
import com.sunfield.windpower.http.HttpClient_;
import com.sunfield.windpower.util.Tools;

import org.androidannotations.annotations.EBean;

/**
 * Created by wxl on 2017/2/6.
 */
@EBean
public class GuideAdapter extends MyRecyclerAdapter<GuideInfo, GuideAdapter.ViewHolder> {
    RecyclerView recyclerView;

    HttpClient client;

    public GuideAdapter(Context context) {
        super(context);
        client = HttpClient_.getInstance_(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (recyclerView == null) {
            recyclerView = (RecyclerView) parent;
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_guide, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final GuideInfo guideInfo = getItem(position);
        holder.iv_icon.setImageResource(guideInfo.getIconRes(context));
        holder.tv_label.setText(guideInfo.getName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (Tools.parseInt(guideInfo.getType())) {
                    //TODO 后续操作
                    case GuideInfo.TYPE_ACTIVITY:
                        context.startActivity(new Intent(context, guideInfo.getActivityClass()));
                        break;
                    case GuideInfo.TYPE_URL:
                        WebViewActivity_.intent(context).url(guideInfo.getUrl()).title(guideInfo.getName()).start();
                        break;
                    case GuideInfo.TYPE_EXIT:
                        removeAllCookie();
//                        EventBus.getDefault().post(new ExitNotification());
                        client.getMenuList();
                        break;
                }
            }
        });
    }

    private void removeAllCookie() {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context.getApplicationContext());
        CookieManager cookieManager = CookieManager.getInstance();

        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();

        cookieManager.removeAllCookie();
        cookieSyncManager.sync();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_icon;
        public TextView tv_label;

        public ViewHolder(View itemView) {
            super(itemView);
            this.iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
            this.tv_label = (TextView) itemView.findViewById(R.id.tv_label);
        }
    }

}
